$(document).ready(function(){
	function Time(time){
			this.hour = parseInt(time.split(':')[0]);
			this.min = parseInt(time.split(':')[1]);

			this.getHour = function(){
				return this.hour;
			}

			this.getMin = function(){
				return this.min;
			}

			this.plus = function(min){
				if(parseInt(this.min) + min >= 60){
					this.hour +=  1;
					this.min = 0;
				} else {
					this.min += min;
				}
			}

			this.getString = function(){
				if(this.min ==0){
					return this.hour + ':' + this.min + '0'; 
				} 
				return this.hour + ':' + this.min;
			}

			this.compare = function(aim){
				if(this.hour > aim.getHour()){
					return true;
				} else if(this.hour < aim.getHour()){
					return false;
				} else {
					if (this.min > aim.getMin()) {
						return true;
					} else {
						return false;
					}
				}
			}

		}

		$('.timepicker').each(function(){
			var time = $(this).data('time');
			var jump = $(this).data('jump');
			var select = $(this).data('select');
			var res = filterTime(time);
			var tmp = '';	
			for (key in res){
				if(res[key] == select){
					tmp += '<option value="'+res[key]+'" selected>' + res[key] + '</option>'
				} else {
					tmp += '<option value="'+res[key]+'">' + res[key] + '</option>'
				}
				
			}
			$(this).html(tmp);

			function filterTime(time){
				var t = time.split(',');
				var res = [];
				for(var i=0; i<t.length; ++i){
					var start = new Time(t[i].split('-')[0]);
					
					var end = new Time(t[i].split('-')[1]);
				
					var addon = start;
				
					while(!addon.compare(end)){
						 
						res.push(addon.getString());
						addon.plus(jump);
						 
						
					}
				}

				return res;
			}


		})
})